//
//  FirmwareViewModel.swift
//  EarbudsFunctions
//
//  Created by 李然 on 2024/5/21.
//

import Foundation
import RxDataSources
import CRPEarPhone
import RxSwift
import RxCocoa

public enum FirmwareSectionType {
    case box
    case ear
}

public struct FirmwareSection {
    public let type: FirmwareSectionType
    public var items: [FirmareSectionItem]
}

extension FirmwareSection: SectionModelType {
    public init(original: FirmwareSection, items: [FirmareSectionItem]) {
        self = original
        self.items = items
    }
}

public class FirmwareUpgradeCellViewModel: DefaultCellViewModel {
    public let newVersion = BehaviorRelay<Version?>(value: nil)
    
    public override init(detail: String = "", arrow: Bool = false) {
        super.init(detail: detail, arrow: arrow)
        self.newVersion.map{ $0 != nil }.bind(to: self.badge).disposed(by: rx.disposeBag)
    }
}

public enum FirmareSectionItem {
    case version(cellModel: DefaultCellViewModel)
    case macAddress(cellModel: DefaultCellViewModel)
    case codeHash(cellModel: DefaultCellViewModel)
    case reset(cellModel: DefaultCellViewModel)
    case upgrade(cellModel: FirmwareUpgradeCellViewModel)
}


public class FirmwareViewModel: ViewModel {
    
    public let dataSource = BehaviorRelay<[FirmwareSection]>(value: [])
    
    public init(viewWillAppear: ControlEvent<Bool>) {
        
        super.init()
        
        viewWillAppear.subscribe(onNext: { [weak self] _ in
            self?.reload()
        }).disposed(by: rx.disposeBag)
    }
    
    private func reload() {
        var section = [FirmwareSection]()
        
        do {
            let ver = DefaultCellViewModel()
            
            let macAddress = DefaultCellViewModel()
            
            let codeHash = DefaultCellViewModel()
            
            let reset = DefaultCellViewModel(arrow: true)
            
            let upgrade = FirmwareUpgradeCellViewModel(arrow: true)
            
            let items = [FirmareSectionItem.version(cellModel: ver),
                         FirmareSectionItem.macAddress(cellModel: macAddress),
                         FirmareSectionItem.codeHash(cellModel: codeHash),
                         FirmareSectionItem.reset(cellModel: reset),
                         FirmareSectionItem.upgrade(cellModel: upgrade)]
            
            section.append(FirmwareSection(type: .box, items: items))
            
            BluetoothManager.shared.queryEarPhoneBoxMacAddress()
                .bind(to: macAddress.detail).disposed(by: macAddress.rx.disposeBag)
            
            let boxVer = BluetoothManager.shared.queryEarPhoneBoxVer().share()
            boxVer.map{ $0.verison }.bind(to: ver.detail).disposed(by: ver.rx.disposeBag)
            boxVer.map{ $0.gitHash }.bind(to: codeHash.detail).disposed(by: codeHash.rx.disposeBag)
            
            if let device = Device.current() {
                BluetoothManager.shared.checkLatest(device.version, device.macAddress)
                    .asObservable().bind(to: upgrade.newVersion).disposed(by: rx.disposeBag)
            }
            
        }
        
        do {
            let ver = DefaultCellViewModel()
            
            let macAddress = DefaultCellViewModel()
            
            let codeHash = DefaultCellViewModel()
            
            let reset = DefaultCellViewModel(arrow: true)
            
            let upgrade = FirmwareUpgradeCellViewModel(arrow: true)
            
            let items = [FirmareSectionItem.version(cellModel: ver),
                         FirmareSectionItem.macAddress(cellModel: macAddress),
                         FirmareSectionItem.codeHash(cellModel: codeHash),
                         FirmareSectionItem.reset(cellModel: reset),
                         FirmareSectionItem.upgrade(cellModel: upgrade)]
            
            section.append(FirmwareSection(type: .ear, items: items))
            
            BluetoothManager.shared.queryEarPhoneMacAddress()
                .bind(to: macAddress.detail).disposed(by: macAddress.rx.disposeBag)
            
            let earVer = BluetoothManager.shared.queryEarPhoneVer().share()
            earVer.map{ $0.version }.bind(to: ver.detail).disposed(by: ver.rx.disposeBag)
            earVer.map{ $0.gitHash }.bind(to: codeHash.detail).disposed(by: codeHash.rx.disposeBag)
        }
        
        self.dataSource.accept(section)
    }
    
}
